/* eslint-disable no-return-assign */

import { userInfoApi, userRoleApi } from '@/api/loginApi'

/* eslint-disable no-undef */
export default {
  // 开启命名空间
  namespaced: true,
  state: {
    token: '',
    userInfo: {},
    userRole: []
  },
  getters: {
    getToken (state) {
      return state.token
    },
    getUserInfo (state) {
      return state.userInfo
    },
    getUserRole (state) {
      return state.userRole
    }
  },
  actions: {
    async userIfoAct ({ commit }, id) {
      const infoResult = await userInfoApi(id)
      let userRole = []
      const userInfo = infoResult.code === 200 ? infoResult.data : {}
      if (infoResult.code === 200) {
        const roleResult = await userRoleApi(infoResult.data.role)

        // eslint-disable-next-line no-const-assign
        userRole = roleResult.code === 200 ? roleResult.data : []
      }
      commit('mutationUserInfo', userInfo)
      commit('mutationUserRole', userRole)
    }
  },
  mutations: {
    mutationToken (state, token) {
      state.token = token
    },
    mutationUserInfo (state, userinfo) {
      state.userinfo = userinfo
    },
    mutationUserRole (state, userRole) {
      state.userRole = userRole
    }
  }

}
